package com.comprehensive.personalnovel.controller;

import com.comprehensive.common.dto.PageResponse;
import com.comprehensive.common.po.Novel;
import com.comprehensive.common.po.NovelChapter;
import com.comprehensive.personalnovel.service.NovelService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/novel")
public class NovelController {
    private final NovelService novelService;
    @Autowired
    public NovelController(NovelService novelService) {
        this.novelService = novelService;
    }
    @GetMapping("/listPage")
    public ResponseEntity<PageResponse<Novel>> listPage(
            @RequestParam(defaultValue = "0") int pageNum,
            @RequestParam(defaultValue = "10") int pageSize
    ) {
        PageResponse<Novel> pageResponse = novelService.listPage(pageNum, pageSize);
        return ResponseEntity.ok(pageResponse);
    }
    @GetMapping("/{id}")
    public ResponseEntity<Novel> queryById(@PathVariable String id) {
        return ResponseEntity.ok(novelService.queryById(id));
    }
    @PostMapping
    public void save(@RequestBody Novel novel) {
        novelService.save(novel);
    }
    @PutMapping
    public void update(@RequestBody Novel novel) {
        novelService.update(novel);
    }
    @DeleteMapping("/{id}")
    public void delete(@PathVariable String id) {
        novelService.delete(id);
    }

    @GetMapping("/chapter/listPage")
    public ResponseEntity<PageResponse<NovelChapter>> listChapterPage(
            @RequestParam(defaultValue = "0") int pageNum,
            @RequestParam(defaultValue = "10") int pageSize,
            @RequestParam String novelId
    ) {
        return ResponseEntity.ok(novelService.listChapterPage(pageNum, pageSize, novelId));
    }
    @GetMapping("/chapter/{id}")
    public ResponseEntity<NovelChapter> queryChapterById(@PathVariable String id) {
        return ResponseEntity.ok(novelService.queryChapterById(id));
    }
    @PostMapping("/chapter")
    public void saveChapter(@RequestBody NovelChapter novelChapter) {
        novelService.saveChapter(novelChapter);
    }
    @PutMapping("/chapter")
    public void updateChapter(@RequestBody NovelChapter novelChapter) {
        novelService.updateChapter(novelChapter);
    }
    @DeleteMapping("/chapter/{id}")
    public void deleteChapter(@PathVariable String id) {
        novelService.deleteChapter(id);
    }
    @PostMapping("/chapter/read")
    public void readNovelChapter(@RequestParam String filePath,@RequestParam String novelId) {
        novelService.readNovelChapter(filePath,novelId);
    }
}
